﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

namespace DotNetCore.MyBlogApi.Data.Attributes
{
    public static class ContextExtensions
    {
        public static string GetTableName(this DbContext context, Type tableType)
        {
            MethodInfo method = typeof(ContextExtensions).GetMethod("GetTableName", new Type[] { typeof(DbContext) })
                .MakeGenericMethod(new Type[] { tableType });
            return (string)method.Invoke(context, new object[] { context });
        }

        //public static string GetTableName<T>(this DbContext context) where T : class
        //{
        //    ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;

        //    return objectContext.GetTableName<T>();
        //}

        //public static string GetTableName<T>(DbContext context) where T : class
        //{
        //    string table = context.Model.FindEntityType(typeof(T)).FindPrimaryKey().Properties
        //   .Select(x => x.Name).Single();
        //    return table;
        //}
    }
}